const ProductModel = require('../../models/ProductModel')



const ProductService = {
    add: async ({ title, introduction, detail, publisher, cover, editTime }) => {
        // console.log("数据库的模型处理");
        try {
            return ProductModel.create({ title, introduction, detail, publisher, cover, editTime })
        } catch (error) {
            throw error
        }

    },
    getAll: async () => {
        try {
            return ProductModel.find({})
        } catch (error) {
            throw error
        }
    },
    getListByPublisher: async ({ publisher }) => {
        try {
            return ProductModel.find({ publisher })
        } catch (error) {
            throw error
        }
    },
    deleteProduct: async ({ _id }) => {
        try {
            return ProductModel.deleteOne({ _id })
        } catch (error) {
            throw error
        }
    },
    getProductById: async ({ _id }) => {
        try {
            return ProductModel.find({ _id })
        } catch (error) {
            throw error
        }
    },
    update: async ({ _id, title, introduction, detail, cover, editTime }) => {
        try {
            if (cover) {
                return ProductModel.updateOne({ _id }, { title, introduction, detail, cover, editTime })

            } else {
                return ProductModel.updateOne({ _id }, { title, introduction, detail, editTime })
            }
        } catch (error) {
            throw error
        }
    }
}




module.exports = ProductService